package com.atguigu.guli.service.cms.controller.api;

import com.atguigu.guli.common.base.result.R;
import com.atguigu.guli.service.cms.entity.Ad;
import com.atguigu.guli.service.cms.service.AdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin
@Api(description="首页")
@RestController
@RequestMapping("/api/cms/ad")
public class ApiAdController {

    @Autowired
    private AdService adService;

    @Autowired
    RedisTemplate redisTemplate;

    @ApiOperation("课程列表")
    @GetMapping("list/{id}")
    public R listByAdTypeId(
            @ApiParam(value = "推荐位",required = true)
            @PathVariable("id") String adTypeId){

        List<Ad> adList = adService.selectByAdTypeId(adTypeId);
        return R.ok().data("items",adList);
    }


    @PostMapping("save-test")
    public R saveAd(@RequestBody Ad ad){

        ValueOperations operations = redisTemplate.opsForValue();//
        operations.set("index::myad",ad);
        return R.ok();
    }

    @GetMapping("get-test/{key}")
    public R getAd(@PathVariable("key") String key){

        Ad o = (Ad)redisTemplate.opsForValue().get(key);
        return R.ok().data("ad",o);
    }

    @DeleteMapping("remove-test/{key}")
    public R removeAd(@PathVariable("key")String key){

        Boolean isDelete = redisTemplate.delete(key);
        if(!isDelete){
            return R.error().message("删除失败");
        }
        return R.ok().message("删除成功");

    }
}
